package {
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import org.blitengine.core.Renderer;
	import org.blitengine.core.Scene;
	import org.blitengine.effects.BlurEffect;
	import org.blitengine.effects.NullEffect;
	import org.blitengine.layers.Layer;
	import org.blitengine.layers.TileMapLayer;
	import org.blitengine.objects.BlitObject;
	import org.blitengine.objects.ClipBlit;

	public class Main extends Sprite
	{
		// Assets
		[Embed(source="assets/test.swf", symbol="Test")]
		private var Test:Class;
		
		[Embed(source="assets/mario.png")]
		private var mario:Class;
		
		[Embed(source="assets/mario_big.png")]
		private var p:Class;
		
		include "../map.as";
		
		// Vars
		
		private var scene:Scene 			= new Scene();
		private var renderer:Renderer 		= new Renderer(stage.stageWidth, stage.stageHeight, scene);
		
		private var bgLayer:Layer 			= new Layer();
		private var mapLayer:Layer			= new TileMapLayer(map, Bitmap(new mario()).bitmapData, 20);
		private var paralaxLayer1:Layer		= new TileMapLayer(map, Bitmap(new p()).bitmapData, 20, 6);
		private var unitsLayer:Layer		= new Layer();
		
		private var player:BlitObject;
		private var playerSpeed:Number = 4;
		
		public function Main()
		{
			stage.frameRate = 60;
			
			unitsLayer.effect = new NullEffect();
			paralaxLayer1.effect = new BlurEffect(8, 8)
			
			paralaxLayer1.paralaxFactor = .2;
			
			unitsLayer.setSize(renderer.bitmapData.width, renderer.bitmapData.height);
			mapLayer.setSize(renderer.bitmapData.width, renderer.bitmapData.height);
			paralaxLayer1.setSize(renderer.bitmapData.width, renderer.bitmapData.height);
			
			renderer.width = stage.stageWidth;
			renderer.height = stage.stageHeight;
			
			player	= new ClipBlit(new Test() as MovieClip, 32, 32, new Point(16, 16));
			renderer.camera.target = player;
			
			unitsLayer.add(player);
			
			scene.addLayer(bgLayer);
			scene.addLayer(paralaxLayer1);
			scene.addLayer(mapLayer);
			scene.addLayer(unitsLayer);
			
			addChild(renderer);
			addChild(new FPSCounter());
			addEventListener(Event.ENTER_FRAME, run);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKey);
		}
		
		private function run(e:Event):void
		{
			var up:Boolean = isDown(Keyboard.UP);
			var down:Boolean = isDown(Keyboard.DOWN);
			var left:Boolean = isDown(Keyboard.LEFT);
			var right:Boolean = isDown(Keyboard.RIGHT);
			if(up)
				player.y -= playerSpeed;
			if(down)
				player.y += playerSpeed;
			if(left)
				player.x -= playerSpeed;
			if(right)
				player.x += playerSpeed;
						
			renderer.render();
		}
		
		//
		
		private var _keyDown:Array = [];
		
		private function onKey(e:KeyboardEvent):void
		{
			_keyDown[e.keyCode] = e.type == KeyboardEvent.KEY_DOWN;
		}
		
		private function isDown(key:int):Boolean
		{
			return _keyDown[key] == true;
		}
	}
}
